if object_id (N'[dbo].[add_new_person]', N'P') IS NOT NULL
    drop procedure [dbo].[add_new_person];
go

/********************************************************
creator: Zoran Arsovski
created: 03.11.2007.
last modifier: unknown
last modified: unknown

description: Procedurata dodava nov red vo tabela Person
			 dokolku istiot ne postoi.
			 Vo sprotivno vrsi update na podatocite.
test: add_new_person '1203980450666', 'Tester', 'Tester', 'test', 'test123'
*********************************************************/

create procedure [dbo].[add_new_person] 
(
	-- Input Parametars
	@embg varchar(20),
	@first_name nvarchar(30),
	@last_name nvarchar(50),
	@username nvarchar(30),
	@password nvarchar(30),
	@address nvarchar(50) = null,
	@city nvarchar(30) = null,   
	@birth_date datetime = null,
	@birth_place nvarchar(30) = null
) as
begin

	if not exists (select 1 from person where embg = @embg)
		insert into person
		(embg, first_name, last_name, username, password, address, city, birth_date, birth_place)
		values
		(@embg, @first_name, @last_name, @username, @password, @address, @city, @birth_date, @birth_place)
	else
		update person
		set first_name   = @first_name,
			last_name    = @last_name,
			username     = @username,
			password     = @password,
			address      = @address,
			city	     = @city,
			birth_date   = @birth_date,
			@birth_place = @birth_place
		where embg = @embg	

end
go		